package com.chen.controller;

import com.chen.pojo.*;
import com.chen.service.BuildingService;
import com.chen.service.RepairService;
import com.chen.service.TeacherService;
import com.chen.service.serviceimpl.TeacherServiceImpl;
import com.chen.utils.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.BitSet;
import java.util.List;

@Controller
public class TeacherController {
    @Autowired
    TeacherService teacherService;
    @Autowired
    BuildingService buildingService;
    @Autowired
    RepairService repairService;

    //获取老师的集合
    @RequestMapping("/getTeacherList.html")
    @ResponseBody
    public PageInfo<Teacher> teacherList(Teacher teacher, @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum){
        System.out.println(pageNum);
        if(teacher != null){
            System.out.println(teacher.getName());
        }
        PageHelper.startPage(pageNum, Constants.PAGESIZE);
        List<Teacher> teachers = teacherService.getTeacherList(teacher);
        PageInfo<Teacher> pageInfo = new PageInfo<Teacher>(teachers);
        return pageInfo;
    }

    //删除老师
    @RequestMapping("/deleteTeacher.html")
    public String deleteTeacher(int id){
        teacherService.deleteById(id);
        return "/WEB-INF/pages/admin/teacherList";
    }

    //去修改老师信息的页面
    @RequestMapping("/to_updateTeacher.html")
    public String toUpdateTeacher(Integer id, HttpServletRequest request){
        Teacher teacher = teacherService.getTeacherById(id);
        List<Building> buildingList = buildingService.getBuildingByTeacherId(id);
        for (Building building : buildingList) {
            System.out.println(building.getId()+building.getBname()+building.getBtype());
        }
        System.out.println(teacher.getName());
        request.setAttribute("teacher",teacher);
        request.setAttribute("buildingList",buildingList);
        return "/WEB-INF/pages/admin/updateTeacher";
    }

    //修改老师
    @RequestMapping("/updateTeacher.html")
    @ResponseBody
    public String updateTeacher(Teacher teacher){
        System.out.println(teacher);
        try {
            teacherService.updateById(teacher);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "defeade";
        }
    }

    //去老师列表
    @RequestMapping("/toMessageInfo.html")
    public String toMessageInfo(){
        return "/WEB-INF/pages/admin/teacherList";
    }
    //去添加老师的页面
    @RequestMapping("/toAddTeacher.html")
    public String toAddTeacher(){
        return "/WEB-INF/pages/admin/addTeacher";
    }

    //添加老师
    @RequestMapping("addTeacher.html")
    @ResponseBody
    public String addTeacher(Teacher teacher){
        System.out.println(teacher.getUsername()+teacher.getPassword()+teacher.getPhone()+teacher.getName()+teacher.getSex());
        try {
            teacherService.addTeacher(teacher);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "defeade";
        }
    }

    //去老师查看报修页面
    @RequestMapping("/to_referreapir.html")
    public String toreferreapir(){
        return "/WEB-INF/pages/teacher/referreapir";
    }

    //显示老师所管楼
    @RequestMapping("/showTeacherBuilding.html")
    @ResponseBody
    public List<Building> showTeacherBuilding(HttpServletRequest request){
        System.out.println("老师查看报修");
        User user = (User)request.getSession().getAttribute(Constants.SESSION_USER);
        List<Building> buildingList = buildingService.getBuildingByTeacherId(user.getId());
        return buildingList;
    }

    //显示老师所管楼的报修表
    @RequestMapping("/teacherReferReapir.html")
    @ResponseBody
    public PageInfo<TeacherShowRepair> teacherReferReapir(HttpServletRequest request,String createDate,@RequestParam(defaultValue = "1") Integer bid,@RequestParam(defaultValue = "1")Integer pageNum){
        System.out.println("显示列表");
        Repair repair = new Repair();
        repair.setBid(bid);
        if(createDate != null && createDate != ""){
            repair.setCreateTime(createDate);
        }
        PageHelper.startPage(pageNum,Constants.PAGESIZE);
        List<TeacherShowRepair> teacherShowRepairList = repairService.getTeacherRepairList(repair);
        PageInfo<TeacherShowRepair> pageInfo = new PageInfo<TeacherShowRepair>(teacherShowRepairList);
        for (TeacherShowRepair teacherShowRepair : pageInfo.getList()) {
            if(teacherShowRepair.getStatus() == null){
                teacherShowRepair.setStatusInfo("未修");
            }else{
                teacherShowRepair.setStatusInfo("已修");
            }
        }
        return pageInfo;
    }

    @RequestMapping("/teacherRecorved.html")
    public String teacherRecorved(Integer id,HttpServletRequest request){
        System.out.println(id+"::::::::::::");
        //UPDATE REPAIR SET STATUS = #{status},TID=#{tid} WHERE ID=#{id}
        Repair repair = new Repair();
        repair.setStatus(1);
        repair.setId(id);
        User user = (User) request.getSession().getAttribute(Constants.SESSION_USER);
        repair.setTid(user.getId());
        try{
            repairService.updateReapir(repair);
            System.out.println("修改成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return "/WEB-INF/pages/teacher/referreapir";
    }
}
